#!/bin/bash
# (C) Copyright 2012,2013 Continuent, Inc - Released under the New BSD License
# Version 1.0.5 - 2013-04-03
cookbook_dir=$(dirname $0)

function gethelp
{
    echo "syntax: $0 {start|stop|print}"
    echo "Starts (or stops) a Bristlecone load in every master deployed in the current topology"
    echo "For more info about this load, see "
    echo  "./bristlecone/bin/concurrent_evaluator.pl --help"
    exit
}

if [ -f CURRENT_TOPOLOGY ]
then
    TOPOLOGY=$(cat CURRENT_TOPOLOGY| tr '[:lower:]' '[:upper:]')
    NODES=NODES_$TOPOLOGY
else
    echo "CURRENT_TOPOLOGY not found" 
    exit
fi

OPERATION=start
if [ -n "$1" ]
then
    OPERATION=$1
fi

case $OPERATION in
    start)
        ;;
    stop)
        ;;
    print)
        ;;
    *)
        echo "unrecognized operation '$OPERATION'"
        gethelp
        ;;
esac

for REQUIRED in USER_VALUES $NODES utilities BOOTSTRAP
do
    if [ ! -f $cookbook_dir/$REQUIRED.sh ]
    then
        echo "could not find $cookbook_dir/$REQUIRED.sh"
        exit 1
    fi
done

. $cookbook_dir/USER_VALUES.sh
. $cookbook_dir/$NODES.sh
. $cookbook_dir/BOOTSTRAP.sh $NODES.sh
. $cookbook_dir/utilities.sh 

DRYRUN=
if [ "$OPERATION" == "print" ]
then
    DRYRUN=1
    OPERATION=start
    VERBOSE=1
fi

[ -n "$VERBOSE" ] && echo "# Determining current roles"

fill_roles

JOB_INFO_PATH=$TUNGSTEN_BASE/tungsten/load

CHECKSUM_DBS=()

for HOST in  ${MASTERS[*]}
do
    unset SKIP
    if [ "$TOPOLOGY" == "STAR" ]
    then
        if [ "$HOST" == "$HUB" ]
        then
            SKIP=1
        fi
    fi
    db=$(echo $HOST | tr '\.' '_' )
    db=$(echo $db | tr '-' '_' )
    if [ "$OPERATION" == "start" ]
    then
        if [ -d $JOB_INFO_PATH/$db ]
        then
            rm -rf $JOB_INFO_PATH/$db/*
        else
            mkdir -p $JOB_INFO_PATH/$db
        fi
    fi

    BRISTLECONE_OPTIONS="--deletes=1 --updates=1 --inserts=1 --test-duration=3600"
    EVALUATOR="$TUNGSTEN_BASE/tungsten/bristlecone/bin/concurrent_evaluator.pl"
    DB_OPTIONS="--host=$HOST --port=$DATABASE_PORT --mysql-defaults-file=$MY_COOKBOOK_CNF --user=$DATABASE_USER --password=$DATABASE_PASSWORD"
    EV_OPTIONS="--continuent-root=$TUNGSTEN_BASE -d $db -s $JOB_INFO_PATH/$db $LOAD_TEST_OPTIONS"
    if [ -n "$CHECK_CONTENTS" ]
    then
        CHECKSUM_DBS[$COUNTER]=$db
        COUNTER=$(($COUNTER+1))
    fi
    V=''
    [ -n "$VERBOSE" ] && [ "$OPERATION" == "stop" ] && V='--verbose'
    CMD="$EVALUATOR $BRISTLECONE_OPTIONS $DB_OPTIONS $EV_OPTIONS $V $OPERATION"
    if [ -n "$VERBOSE" ]
    then
        if [ -z "$SKIP" ]
        then
            echo "$CMD" | perl -pe 's/ -/ \\\n\t-/g'
        fi
    fi
    if [ -z "$DRYRUN" ]
    then
        if [ -z "$SKIP" ]
        then
            $CMD
        fi
    fi
done

function check_contents
{
    echo ""
    for DB in ${CHECKSUM_DBS[*]}
    do
        for HOST in ${ALL_NODES[*]}
        do
            echo -n "# COUNT host $HOST - $DB - "
            $MYSQL -h $HOST -BN -e "select count(*) from $DB.tbl3"
        done
    done

    for DB in ${CHECKSUM_DBS[*]}
    do
        for HOST in ${ALL_NODES[*]}
        do
            echo -n "# CRC host $HOST - "
            $MYSQL -h $HOST -BN -e "checksum table $DB.tbl3"
        done
    done
}

case "$OPERATION" in
    start) 
        touch $cookbook_dir/data_loading
        ;;
    stop)
        if [ -n "$CHECK_CONTENTS" ]
        then
            sleep 1
            check_contents
        fi
        rm -f $cookbook_dir/data_loading
        ;;
esac

